#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 10010;
const int maxk = 105;
struct customer
{
    int time, p, tag;
    int hour, min, sec;
    int wait;
};
bool cmp(customer c1, customer c2)
{
    return c1.time < c2.time;
}
int main()
{
    // freopen("case.txt", "r", stdin);

    int n, k, m, vips[maxk] = {0};
    int stime = 8 * 3600, etime = 21 * 3600;
    customer *c;
    vector<customer> cus;

    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        c = new customer();
        scanf("%02d:%02d:%02d %d %d", &c->hour, &c->min, &c->sec, &c->p, &c->tag);
        //时间最多为2小时
        if (c->p > 120)
            c->p = 120;
        c->time = c->hour * 3600 + c->min * 60 + c->sec;
        c->wait = 0;
        cus.push_back(*c);
    }
    scanf("%d %d", &k, &m);
    int tvip;
    for (int i = 0; i < m; i++)
    {
        scanf("%d", &tvip);
        vips[tvip - 1] = 1;
    }
    sort(cus.begin(), cus.end(), cmp);
    if (n == 0 || k == 0)
        return 0;

    vector<int> tables(k, 28800);
    int count[maxk] = {0};
    for (int it = 0; it < cus.size(); it++)
    {
        int idx = 0, minfinish = tables[0], vidx;

        //选出目前最先开始的桌子
        for (int i = 0; i < k; i++)
        {
            if (minfinish > tables[i])
            {
                minfinish = tables[i];
                idx = i;
            }
        }

        //开放时间相同，vip用户优先选择vip桌
        if (cus[it].tag)
            for (int i = 0; i < k; i++)
            {
                if (vips[i] && minfinish == tables[i])
                {
                    idx = i;
                    break;
                }
            }

        //当前桌是vip桌且处于等待情况下，vip用户可以插队
        if (vips[idx] && !cus[it].tag)
        {
            for (int vit = it + 1; vit < cus.size(); vit++)
            {
                if (cus[vit].tag && tables[idx] > cus[vit].time)
                {
                    customer ct = cus[vit];
                    for (int j = vit; j > it; j--)
                        cus[j] = cus[j - 1];
                    cus[it] = ct;
                    break;
                }
            }
        }

        if (tables[idx] <= cus[it].time)
        {
            tables[idx] = cus[it].time + cus[it].p * 60;
        }
        else
        {
            cus[it].wait = tables[idx] - cus[it].time;
            tables[idx] += cus[it].p * 60;
        }

        int svtime = cus[it].time + cus[it].wait;
        int h, m, s;
        h = svtime / 3600;
        svtime = svtime % 3600;
        m = svtime / 60;
        s = svtime % 60;
        if (cus[it].time + cus[it].wait < etime)
        {
            count[idx]++;
            printf("%02d:%02d:%02d %02d:%02d:%02d %.0f\n", cus[it].hour, cus[it].min, cus[it].sec, h, m, s, round(cus[it].wait / 60.0));
        }
    }
    for (int i = 0; i < k; i++)
    {
        if (i == 0)
            cout << count[i];
        else
            cout << " " << count[i];
    }
    return 0;
}